package com.boot.web.controller;

import java.util.Date;
import java.util.Map;

import com.boot.web.dao.AccountDao;
import com.boot.web.dao.AccountRecordDao;
import com.boot.web.dao.CustomerDao;
import com.boot.web.entity.Account;
import com.boot.web.entity.AccountRecord;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.boot.communal.paging.InitPaging;
import com.boot.communal.response.ResponseServer;
import com.boot.web.entity.CardWithdrawal;
import com.boot.web.service.CardWithdrawalService;

/**
 * 银行卡提现记录控制器
 * @author 【zhouJie】
 * @date 2021-07-30 16:48:47
 */
@RestController
@RequestMapping("/cardWithdrawal")
public class CardWithdrawalController {

    @Autowired
    private CardWithdrawalService cardWithdrawalService;

    @Autowired
    private AccountDao accountDao;

    @Autowired
    private AccountRecordDao accountRecordDao;

    @Autowired
    private CustomerDao customerDao;

    @PostMapping("/save")
    public ResponseServer save(CardWithdrawal cardWithdrawal) {
        return ResponseServer.success("添加成功", cardWithdrawalService.save(cardWithdrawal));
    }

    @PutMapping("/update")
    public ResponseServer update(CardWithdrawal cardWithdrawal) {
        CardWithdrawal cw = cardWithdrawalService.getById(cardWithdrawal.getId());
        if("0".equals(cw.getStatus()) && "2".equals(cardWithdrawal.getStatus())){
            // 提现失败 把余额退回去
            Account account = accountDao.queryAccountByUserId(cardWithdrawal.getUserId());
            account.setVoucherBalance(account.getVoucherBalance().add(cardWithdrawal.getAmount()));
            accountDao.update(account);

            AccountRecord ar1 = new AccountRecord();
            ar1.setIntoUserId(cardWithdrawal.getUserId());
            ar1.setUpdateTime(new Date());
            ar1.setType("0");
            ar1.setAmount(cardWithdrawal.getAmount());
            ar1.setNowAmount(account.getVoucherBalance());
            ar1.setSign("1");
            ar1.setInfo(customerDao.queryById(cardWithdrawal.getUserId()).getAgentCode() + "提现金额为：" + cardWithdrawal.getAmount() + "，已退回");
            accountRecordDao.save(ar1);
            cardWithdrawalService.update(cardWithdrawal);
            return ResponseServer.success("修改成功");
        }
        if("1".equals(cw.getStatus()) && "2".equals(cardWithdrawal.getStatus())){
            return ResponseServer.error("已到账的不能在修改为提现失败!!!");
        }
        if("2".equals(cw.getStatus()) && "1".equals(cardWithdrawal.getStatus())){
            return ResponseServer.error("提现失败的不能在修改为已到账!!!");
        }
        if("0".equals(cw.getStatus()) && "1".equals(cardWithdrawal.getStatus())){
            cardWithdrawal.setConfirmTime(new Date());
            cardWithdrawalService.update(cardWithdrawal);
            return ResponseServer.success("修改成功");
        }
        return ResponseServer.error("提现状态不正确!!!");
    }

    @DeleteMapping("/delete/{id}")
    public ResponseServer delete(@PathVariable Integer id) {
        return ResponseServer.success("删除成功", cardWithdrawalService.delete(id));
    }

    @DeleteMapping("/batchDelete/{ids}")
    public ResponseServer batchDelete(@PathVariable Integer[] ids) {
        return ResponseServer.success("批量删除成功", cardWithdrawalService.batchDelete(ids));
    }

    @GetMapping("/getPageList")
    public ResponseServer getPageList(@RequestParam Map<String, Object> params) {
        InitPaging.init(params);
        return ResponseServer.success(new PageInfo(cardWithdrawalService.getPageList(params)));
    }

    @GetMapping("/getAll")
    public ResponseServer getAll() {
        return ResponseServer.success(cardWithdrawalService.getAll());
    }

    @GetMapping("/getById/{id}")
    public ResponseServer getById(@PathVariable Integer id) {
        return ResponseServer.success(cardWithdrawalService.getById(id));
    }

}
